001 /**
002 * Created by IntelliJ IDEA.
003 * User: Wei Wang
004 * Date: Apr 1, 2003
005 * Time: 8:03:20 PM
006 */
007
008 package EVolve.util.equators;
009
010 import EVolve.util.HelperFuncs;
011
012 import java.util.*;
013
014 public class UnorderedUnlimitedSet extends Set{
015 private HashMap data;
016
017 public UnorderedUnlimitedSet() {
018 data = new HashMap();
019 }
020
021 public int size() {
022 return data.size();
023 }
024
025 public long getElement(int i) {
026 return ((Long)data.get(new Integer(i))).longValue();
027 }
028
029 public boolean setFull() {
030 return false;
031 }
032
033 public long getEntityId(int i) {
034 return getElement(i);
035 }
036
037 public void addElement(long id) {
038 if (!data.containsValue(new Long(id)))
039 data.put(new Integer(data.size()),new Long(id));
040 }
041
042 public Set newSet() {
043 return new UnorderedUnlimitedSet();
044 }
045
046 public Set union(Set set) {
047 Set result = new UnorderedUnlimitedSet();
048 HashMap setData[] = new HashMap[2];
049
050 setData[0] = data;
051 setData[1] = ((UnorderedUnlimitedSet)set).data;
052
053 for (int i=0; i<setData.length; i++) {
054 Iterator it = setData[i].keySet().iterator();
055 while (it.hasNext()) {
056 result.addElement(((Long)setData[i].get(it.next())).longValue());
057 }
058 }
059
060 return result;
061 }
062
063 public Set intersection(Set set) {
064 HashMap otherData = ((UnorderedUnlimitedSet)set).data;
065 Set result = new UnorderedUnlimitedSet();
066
067 Iterator it = data.keySet().iterator();
068 while (it.hasNext()) {
069 Long value = (Long)data.get(it.next());
070 if (otherData.containsValue(value))
071 result.addElement(value.longValue());
072 }
073
074 return result;
075 }
076
077 public boolean exist(long element) {
078 return data.containsValue(new Long(element));
079 }
080
081 public boolean equals(Set set, float percent) {
082 Set union = union(set);
083 Set intersection = intersection(set);
084
085 return ((float)intersection.size()/(float)union.size()) >= percent;
086
087 }
088
089 public float getOrderedMatch(UnorderedUnlimitedSet next) {
090 HashMap source = data, target = next.data;
091 Iterator it = source.keySet().iterator();
092 int accum = 0, count = 0;
093 float result;
094
095 while (it.hasNext()) {
096 Long mappedId = (Long)source.get(it.next());
097 //if (target.containsValue(mappedId)) continue;
098 accum += mappedId.intValue();
099 count++;
100 }
101 result = (float)accum/(float)count;
102
103 accum = count = 0;
104 it = target.keySet().iterator();
105 while (it.hasNext()) {
106 Long mappedId = (Long)target.get(it.next());
107 //if (source.containsValue(mappedId)) continue;
108 accum += mappedId.intValue();
109 count++;
110 }
111 result = Math.abs(result - (float)accum/(float)count);
112
113 return result;
114 }
115
116 public long getMaxEntityId() {
117 int maxId = 0;
118
119 Iterator it = data.keySet().iterator();
120 while (it.hasNext()) {
121 Long mappedId = (Long)data.get(it.next());
122 if (maxId < mappedId.intValue())
123 maxId = mappedId.intValue();
124 }
125 return maxId;
126 }
127
128 public long getHashValue() {
129 return 0;
130 }
131
132 public Object clone() {
133 UnorderedUnlimitedSet o = (UnorderedUnlimitedSet)super.clone();
134 o.data = HelperFuncs.cloneHashMap(data);
135
136 return o;
137 }
138 }